package com.health.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.demo.common.vo.Result;
import com.health.demo.entity.User;
import com.health.demo.dao.UserDao;
import com.health.demo.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 洛璃依
 * @since 2022-12-02 01:33:35
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired(required = false)
    private UserDao userDao;

    @Override
    public Result<User> queryUser(User user) {
        //1.创建分页对象
        Page<User> userPage = new Page<>(user.getPage(),user.getRow());
        //(1)构造条件构造器
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        //(2)模糊查询 like
        userQueryWrapper.like(user.getUserName()!=null&&!user.getUserName().equals("")
                ,"user_name",user.getUserName());
        //(3)精确查询 eq

        //2.获取查询对象
        Page<User> userPageList = userDao.selectPage(userPage, userQueryWrapper);
        if (userPageList.getRecords().size()>0){
            //创建页集合
            HashMap<String, Object> map = new HashMap<>();
            //总页数
            map.put("list",userPageList.getRecords());
            //总条数
            map.put("total",userPageList.getTotal());
//            return Result.ok(map);
        }
        return Result.error();
    }

    @Override
    public Result<User> addUser(User user) {
        int insert = userDao.insert(user);
        if (insert>0){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @Override
    public Result<User> delUser(Integer userId) {
        int i = userDao.deleteById(userId);
        if (i>0){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @Override
    public Result<User> updateUser(User user) {
        int i = userDao.updateById(user);
        if (i>0){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
}
